package com.zzgc.modules.controller;

import com.alibaba.fastjson.JSON;
import com.zzgc.common.entity.School;
import com.zzgc.common.entity.SchoolGrade;
import com.zzgc.common.entity.SchoolGradeClass;
import com.zzgc.common.entity.StudentArchives;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.HttpClientUtil;
import com.zzgc.modules.param.Sort;
import com.zzgc.modules.param.StudentArchivesParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 学生档案控制类
 *
 * @Author:
 * @CreateTime: 2022-12-02  09:22
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("archive")
@Api(tags = "学生档案控制类")
public class StudentArchivesController extends BaseController {

    @GetMapping("get")
    @ApiOperation(value = "获取学生视力档案")
    public Result getStudentArchives(StudentArchivesParam archivesParam) {
        archivesParam.setTableName(super.getStudentTable());
        archivesParam.setSorts(Arrays.asList(new Sort("a.s_year","DESC"),new Sort("a.s_number")));
        List<StudentArchivesParam> archivesParams = studentArchivesService.queryAllByLimitArchives(archivesParam);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", archivesParams.size() == 0 ? 0 : archivesParams.get(0).getTotal());
        map.put("data", archivesParams);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(map)
                .build();
    }

    @GetMapping("getAll")
    @ApiOperation(value = "获取数据库所有学校的档案")
    public Result getSchoolAll(StudentArchivesParam archivesParam){
        List<String> list = new ArrayList<>();
        if (!"1".equals(super.getUserRole())){ // 根据权限获取学生信息
            School school1 = new School();
            // 根据角色查询学校
            if (super.getDepartId() != null) {
                school1.setSDepartId(Long.valueOf(super.getDepartId()));
            }else {
                return Result.builder().code(500).msg("暂无部门信息！").build();
            }
            List<School> schools = schoolService.queryByPage(school1);
            for (School school : schools) {
                // archives_411624_17 添加区县下的学校
                list.add("archives_" + school.getSArea() + "_" + school.getId());
            }
        }else { // 需要获取数据库所有学校表
            list = schoolTableDao.queryAllTableName(super.getDatabaseName(), super.getTableName());
        }
        if (list.size() < 1) {
            return Result.builder().code(500).msg("暂无信息").build();
        }
        archivesParam.setTableNames(list);
        // 通过学校, 年级, 班级id查询名字
        if (archivesParam.getSSchoolId() != null){
            School school = schoolService.queryById(archivesParam.getSSchoolId());
            archivesParam.setSSchoolName(school.getSName());
        }
        if (archivesParam.getSGradeId() != null){
            SchoolGrade schoolGrade = schoolGradeService.queryById(archivesParam.getSGradeId());
            archivesParam.setSGradeName(schoolGrade.getSName());
        }
        if (archivesParam.getSClassName() != null){
            SchoolGradeClass schoolGradeClass = schoolGradeClassService.queryById(archivesParam.getSClassId());
            archivesParam.setSClassName(schoolGradeClass.getSName());
        }
        List<StudentArchivesParam> studentArchivesParams = studentArchivesService.queryAllSchoolByLimitArchives(archivesParam);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", studentArchivesParams.size() == 0 ? 0 : studentArchivesParams.get(0).getTotal());
        map.put("data", studentArchivesParams);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(map)
                .build();
    }

    public static void main(String[] args) {
        ArrayList<Object> objects = new ArrayList<>();
        for (int i = 1; i < 100; i++) {
            objects.add(i);
        }
        System.out.println("objects = " + objects);
        List<Object> page = page(objects, 2, 15);
        System.out.println("page = " + page);
    }

    public static List<Object> page(List<Object> list, Integer page, Integer size){
        page = (page - 1) * size;
        size = page + size;
        return list.subList(page, size);
    }
}
